//news.js
//获取应用实例
var app = getApp();
Page({
  data: {
    page: 0,
    list: [
      { id: 1, 'type': 'all', name: '全部', color:'#f2cab8', storage: [], url: '', enabled: { guest: true} },
      { id: 2, 'type': 'text', name: '文字', color: '#ffcb63', storage: [], url: '', enabled: { guest: true} },
      { id: 3, 'type': 'image', name: '图片', color: '#7acfa6', storage: [], url: '', enabled: { guest: true} },
      { id: 4, 'type': 'gif', name: '动图', color: '#73b4ef', storage: [], url: '', enabled: { guest: true} },
      { id: 5, 'type': 'video', name: '视频', color: '#9f8bea', storage:[], url: '', enabled: {guest:true} },
    ],
    'active': {
      id: 1,
      'type': 'all',
      data: [],
      showMore: true,
      remind: '上滑加载更多'
    },
    loading: false,
    user_type: 'guest',
    disabledRemind: false
  },
  onLoad: function(){
    this.getNewsList();
  },
  onShow: function(){
    this.setData({user_type: 'guest'});
  },
  //下拉更新
  onPullDownRefresh: function(){
    var _this = this;
    _this.setData({
      'loading': true,
      'active.data': [],
      'active.showMore': true,
      'active.remind': '上滑加载更多',
      'page': 0
    });
    _this.getNewsList();
  },
  //上滑加载更多
  onReachBottom: function(){
    var _this = this;
    if(_this.data.active.showMore){
      _this.getNewsList();
    }
  },
  //获取新闻列表
  getNewsList: function(typeId){
    var _this = this;
    typeId = typeId || _this.data.active.id;
    _this.setData({
      'active.remind': '正在加载中'
    });
    wx.showNavigationBarLoading();
    wx.request({
      url: 'https://www.apiopen.top/satinGodApi',
      data: {
        page: _this.data.page + 1,
        type: typeId
      },
      success: function(res){
        if (res.data && res.data.code === 200){
          if(_this.data.active.id != typeId){ return false; }
          if (res.data.data.length > 0){
            res.data.data.forEach(function (item) {
              let text = item['text'];
              item['title'] = text.length < 14 ? text.replace(/[\r\n]/g, "") : text.substr(0, 14).replace(/[\r\n]/g, "") + "...";
            });
            if (!_this.data.page){
                var data = {
                  'page': _this.data.page + 1,
                  'active.data': res.data.data,
                  'active.showMore': true,
                  'active.remind': '上滑加载更多',
                };
                _this.setData(data);
            }else{
              _this.setData({
                'page': _this.data.page + 1,
                'active.data': _this.data.active.data.concat(res.data.data),
                'active.showMore': true,
                'active.remind': '上滑加载更多',
              });
            }
          }else{
            _this.setData({
              'active.showMore': false,
              'active.remind': '没有更多啦'
            });
          }
        }else{
          app.showErrorModal(res.data.message);
          _this.setData({
            'active.remind': '加载失败'
          });
        }
      },
      fail: function(res){
        app.showErrorModal(res.errMsg);
        _this.setData({
          'active.remind': '网络错误'
        });
      },
      complete: function(){
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        _this.setData({
          loading: false
        });
      }
    });
  },
  //获取焦点
  changeFilter: function(e){
    this.setData({
      'active': {
        'id': e.target.dataset.id,
        'type': e.target.id,
        data: [],
        showMore: true,
        remind: '上滑加载更多'
      },
      'page': 0
    });
    wx.setNavigationBarTitle({
      title: e.target.dataset.name,
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: e.target.dataset.color,
    })
    this.getNewsList(e.target.dataset.id);
  },

});